﻿#ifndef __SS_MULITI_LINR_BUFFER_H__
#define __SS_MULITI_LINR_BUFFER_H__
#include "ssBuffer.h"

namespace StarSeeker
{

class ssMultiLineBuffer
{
public:
	//迭代器
	class Iterator
	{
		friend class ssMultiLineBuffer;
	public:
		Iterator() :nSize(0), pData(0), traversalCount(0)
		{
		}
		Iterator(const Iterator& i) :nSize(i.nSize), pData(i.pData), traversalCount(i.traversalCount)
		{
		}
		~Iterator()
		{
			Clear();
		}
		void operator= (const Iterator& i)
		{
			nSize = i.nSize;
			pData = i.pData;
			traversalCount = i.traversalCount;
		}
		ulint Size()
		{
			return nSize;
		}
		const char* Data()
		{
			return pData;
		}
		bool IsVaild() const
		{
			return pData != 0;
		}
		void Clear()
		{
			nSize = 0;
			pData = 0;
			traversalCount = 0;
		}
	private:
		ulint nSize;
		char* pData;
		ulint traversalCount;	//遍历用的计数
	};


	ssMultiLineBuffer();
	ssMultiLineBuffer(const ssMultiLineBuffer &oc);
	~ssMultiLineBuffer();
	void operator = (const ssMultiLineBuffer &oc);
	//创建缓存
	bool Init(ulint size);
	//读入外部缓存，按照本容器的格式
	bool FormatIn(char *data, ulint size);
	//保存一条数据
	bool PushBack(const void *data, ulint len);
	//返回已保存的数据条数
	ulint Count() const;
	//返回已使用的缓存长度
	ulint Size() const;
	//返回剩余的缓存长度
	ulint RemainSize();
	//返回内存地址
	const char* Buf() const;
	//清空缓存
	void Empty();
	//释放内存
	void Clear();
	//遍历开始
	Iterator Begin();
	//遍历下一条
	bool Next(Iterator& itr);
private:
	//从新分配大小
	bool Resize(ulint newSizew);
	//从另一个缓存复制数据
	void CopyFrom(const ssMultiLineBuffer &oc);
private:
	ssBuffer	m_pMem;				//缓存
	ulint		m_count;			//内容数量
};

}
#endif
